﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hongapo_Common;

public partial class sysadmin_login :System.Web.UI.Page
{
   private Hongapo_Services.Users.Users service = new Hongapo_Services.Users.Users();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Out();
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Login();
    }

    #region 退出
    private void Out()
    {
        string action = Request["action"];
        if (!string.IsNullOrEmpty(action))
        {
            if (action.Equals("out"))
            {
                PageBase.CurrentUser = null;
            }
        }
    }
    #endregion

    #region 登录
    private void Login()
    {
        string password = StringUtils.FilterVal(this.password.Value.Trim());
        string username = StringUtils.FilterVal(this.username.Value.Trim());
        if (!string.IsNullOrEmpty(password) && !string.IsNullOrEmpty(username))
        {
            DataTable dt = service.GetList(" Isopen=1 and Account='"+username+"' and password='"+password+"'").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                Hongapo_Model.Users.MUsers model = service.GetModel(TypeConvert.ToInt32(dt.Rows[0]["ID"]));
                PageBase.CurrentUser = model;
                Response.Redirect("index.html");
            }
            else {
                ClientScript.RegisterStartupScript(this.GetType(), "message", "<script language='javascript' defer>alert('用户名或密码错误！');</script>");
              
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "message", "<script language='javascript' defer>alert('用户名密码为空或包含特效字符！');</script>");
      
            return;
        }

    }
    #endregion
}
